<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 4.3.&nbsp; Conditionals</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-4-SECT-3"></a>
<h3 id="title-IDAIH0EF" class="docSection1Title">4.3. Conditionals</h3>
<a name="IDX-CHP-4-0143"></a> 
<a name="IDX-CHP-4-0144"></a> 

<p class="docText">Since we've been talking about variables, we should also mention conditionals, which, like variables, form a building block in our foundation of PHP development. They alter a script's behavior according to the criteria set in the code. There are three primary elements of conditionals in PHP:</p>
<ul><li><p class="docList"><tt>if</tt></p></li><li><p class="docList"><tt>switch</tt></p></li><li><p class="docList"><tt>? : :</tt> (shorthand for an <tt>if</tt> statement)</p></LI></ul>
<p class="docText">PHP also supports a conditional called a <tt>switch</tt> statement. The <tt>switch</tt> statement is useful when you need to take different action based on the contents of a variable that may be set to one of a list of values.</p>
<a name="learnphpmysql-CHP-4-SECT-3.1"></a>
<H4 id="title-IDAWI1EF" class="docSection2Title">4.3.1. The if Statement</H4>
<a name="IDX-CHP-4-0145"></a> 
<a name="IDX-CHP-4-0146"></a> 

<p class="docText">The <tt>if</tt> statement offers the ability to execute a block of code, if the supplied condition is <tt>trUE</tt>; otherwise, the code block doesn't execute. The type of condition can be any expression, including tests for nonzero, null, equality, variables, and returned values from functions.</P>
<p class="docText">No matter what, every single conditional you create includes a conditional clause. If a condition is true, the code block in curly brackets (<tt>{}</tt>) is executed. If not, PHP ignores it and moves to the second condition and continues through as many clauses as you write until PHP hits an <tt>else</tt>, then it automatically executes that block.</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-4-FIG-2">Figure 4-2</a> demonstrates how an <tt>if</tt> statement works. The <tt>else</tt> block always needs to come last and be treated as if it's the default action. This is similar to the semicolon (<tt>;</tt>), which acts as the end of a sentence. Common true conditions are:</p>
<ul><LI><p class="docList"><tt>$var</tt>, if <tt>$var</tt> has a value other than the empty set (<tt>0</tt>), an empty string, or <tt>NULL</tt></p></li><LI><p class="docList"><tt>isset ($var)</tt>, if <tt>$var</tt> has any value other than <tt>NULL</tt>, including the empty set or an empty string</p></li><LI><p class="docList"><tt>trUE</tt> or any variation thereof</p></li></ul>
<a name="learnphpmysql-CHP-4-FIG-2"></a><P><center>
<H5 class="docFigureTitle">Figure 4-2. Execution branching based on an expression</h5>
<img border="0" alt="" width="350" height="251" SRC="images/learnphpmysql_0402.jpg">
</center></p><br>
<p class="docText">We haven't talked about the second bullet point, <tt>isset</tt>, yet. <tt>isset()</tt> is a function that checks whether a variable is set. A set variable is one that has a value other than <tt>NULL</tt>. <a class="docLink" href="learnphpmysql-CHP-4-SECT-2.html#learnphpmysql-CHP-4-TABLE-2">Table 4-2</a> shows comparative and logical operators, which can be used in conjunction with parentheses, <tt>()</tt>, to create more complicated expressions.</p>
<p class="docText">The syntax for the <tt>if</tt> statement is:</P>
<pre>
if (conditional expression)
  {
  block of code;
  }
</pre><br>

<p class="docText">If the expression in the conditional block evaluates to <tt>TRUE</tt>, the block of code after it executes. In this example, if the variable <tt>$username</tt> is set to <tt>Admin</tt>, a welcome message is printed. Otherwise, nothing happens.</p>
<pre>
if ($username=="Admin") {
  echo ('Welcome to the admin page.');
  }
</pre><BR>

<p class="docText">The curly brackets aren't needed if you want to execute only one statement, but it's good practice to always use them, as it makes the code easier to read and more resilient to change.</P>
<a name="learnphpmysql-CHP-4-SECT-3.1.1"></a>
<H5 id="title-IDANM1EF" class="docSection3Title">4.3.1.1. The else statement</h5>
<a name="IDX-CHP-4-0147"></a> 
<a name="IDX-CHP-4-0148"></a> 
<a name="IDX-CHP-4-0149"></a> 
<a name="IDX-CHP-4-0150"></a> 

<p class="docText">The <tt>else</tt> statement (see <a class="docLink" href="#learnphpmysql-CHP-4-EX-6">Example 4-6</a>) provides for a default block of code that executes if the condition returned is <tt>FALSE</tt>. It must always be part of an <tt>if</tt> statement, as it doesn't take a conditional itself.</p>
<a name="learnphpmysql-CHP-4-EX-6"></a><h5 id="title-IDATN1EF" class="docExampleTitle">Example 4-6. else and if statements</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
if ($username == "Admin"){
    echo ('Welcome to the admin page.');
}
else {
    echo ('Welcome to the user page.');
}
</pre><br>

</td></tr></table></p>
<p class="docText">Remember to close out the code block from the <tt>if</tt> conditional if you used brackets to start the block of code. Similar to the <tt>if</tt> block, the <tt>else</tt> block should also use curly brackets to begin and end the code.</p>

<a name="learnphpmysql-CHP-4-SECT-3.1.2"></a>
<h5 id="title-IDAGO1EF" class="docSection3Title">4.3.1.2. The elseif statement</h5>
<p class="docText">All of this is great except for when you want to test for several conditions at a time. To do this, you can use the <tt>elseif</tt> statement. It allows for testing of additional conditions until one is found to be true or you hit the <tt>else</tt> block. Each <tt>elseif</tt> has its own code block that comes directly after the <tt>elseif</tt> condition. The <tt>elseif</tt> must come after the <tt>if</tt> statement and before an <tt>else</tt> statement if one exists.</P>
<p class="docText">The <tt>elseif</tt> structure is a little complicated, but <a class="docLink" href="#learnphpmysql-CHP-4-EX-7">Example 4-7</a> should help you understand it.</p>
<a name="learnphpmysql-CHP-4-EX-7"></a><h5 id="title-IDAHP1EF" class="docExampleTitle">Example 4-7. Checking multiple conditions</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
if ($username == "Admin"){
    echo ('Welcome to the admin page.');
}
elseif ($username == "Guest"){
    echo ('Please take a look around.');
}
else {
    echo ("Welcome back, $username.");
}
</pre><br>

</td></tr></table></P>
<p class="docText">Here you can check for and take different action based on two values for <tt>$username</tt>. Then you also have the option to do something else if the <tt>$user_name</tt> isn't one of the first two.</p>
<p class="docText">The next construct builds on the concepts of the <tt>if</tt>/<tt>else</tt> statement, but it allows you to efficiently check the results of an expression to many values without having a separate <tt>if</tt>/<tt>else</tt> for each value.</p>


<a name="learnphpmysql-CHP-4-SECT-3.2"></a>
<H4 id="title-IDAHQ1EF" class="docSection2Title">4.3.2. The ? Operator</h4>
<a name="IDX-CHP-4-0151"></a> 
<a name="IDX-CHP-4-0152"></a> 
<a name="IDX-CHP-4-0153"></a> 

<p class="docText">The <tt>?</tt> operator is a ternary operator, meaning it takes three operands. It works like an <tt>if</tt> statement but returns a value from one of the two expressions. The conditional expression determines the value of the expression. A colon (<tt>:</tt>) is used to separate the expressions:</p>
<pre>
{expression} ? return_when_expression_true : return_when_expression_false;
</pre><BR>

<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-4-EX-8">Example 4-8</a> tests a value and returns a different string based on it being <tt>trUE</tt> or <tt>FALSE</tt>.</p>
<a name="learnphpmysql-CHP-4-EX-8"></a><h5 id="title-IDARR1EF" class="docExampleTitle">Example 4-8. Using the ? operator to create a message</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
$logged_in = TRUE;
$user = "Admin";
$banner = <b>($logged_in==TRUE)?"Welcome back $user!":"Please login.";</b>
echo "$banner";
?&gt;
</pre><br>

</td></TR></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-4-EX-8">Example 4-8</a> produces:</P>
<pre>
Welcome back Admin!
</pre><br>

<p class="docText">This can be pretty useful for checking errors. Now, let's look at a statement that lets you check an expression against a list of possible values to pick the executable code.</P>

<a name="learnphpmysql-CHP-4-SECT-3.3"></a>
<H4 id="title-IDAJS1EF" class="docSection2Title">4.3.3. The switch Statement</H4>
<a name="IDX-CHP-4-0154"></a> 

<p class="docText">The <tt>switch</tt> statement compares an expression to numerous values. It's very common to have an expression, such as a variable, for which you'll want to execute different code for each value stored in the variable. For example, you might have a variable called <tt>$action</tt>, which may have the values <tt>add, modify</tt>, and <tt>delete</tt>. The <tt>switch</tt> statement makes it easy to define a block of code to execute for each of those values.</p>
<p class="docText">To illustrate the difference between using the <tt>if</tt> statement and <tt>switch</tt> to test a variable for several values, we'll show you the code for the <tt>if</tt> statement (in <a class="docLink" href="#learnphpmysql-CHP-4-EX-9">Example 4-9</a>), and then for the <tt>switch</tt> statement (in <a class="docLink" href="#learnphpmysql-CHP-4-EX-10">Example 4-10</a>).</p>
<a name="learnphpmysql-CHP-4-EX-9"></a><h5 id="title-IDAWT1EF" class="docExampleTitle">Example 4-9. Using if to test for multiple values</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
if ($action == "ADD") {
    echo "Perform actions for adding.";
    echo "As many statements as you like can be in each block.";
}
elseif ($action == "MODIFY") {
    echo "Perform actions for modifying.";
}
elseif ($action == "DELETE") {
    echo "Perform actions for deleting.";
}
</pre><br>

</td></tr></table></p>
<a name="learnphpmysql-CHP-4-EX-10"></a><h5 id="title-IDA5T1EF" class="docExampleTitle">Example 4-10. Using switch to test for multiple values</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
switch ($action) {
    case "ADD":
        echo "Perform actions for adding.";
        echo "As many statements as you like can be in each block.";
        break;
    case "MODIFY":
        echo "Perform actions for modifying.";
        break;
    case "DELETE":
        echo "Perform actions for deleting.";
        break;
}
</pre><br>

</TD></TR></table></p>
<p class="docText">The <tt>switch</tt> statement works by taking the value after the <tt>switch</tt> keyword and comparing it to the cases in the order they appear. If no case matches, no code is executed. Once a case matches, the code is executed. The code in subsequent cases also executes until the end of the <tt>switch</tt> statement or until a <tt>break</tt> keyword. This is useful for processes that have several sequential steps. If the user has already done some of the steps, he can jump into the process where he left off.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText">The expression after the <tt>switch</tt> statement must evaluate to a simple type like a number, integer, or string. An array can be used only if a specific member of the array is referenced as a simple type.</p>
</td></TR></table></td></tr></table></p><br>
<p class="docText">There are numerous ways to tell PHP to not execute cases besides the matching case.</P>
<a name="learnphpmysql-CHP-4-SECT-3.3.1"></a>
<H5 id="title-IDA5U1EF" class="docSection3Title">4.3.3.1. Breaking out</h5>
<p class="docText">If you want only the code in the matching block to execute, you can place a <tt>break</tt> keyword at the end of that block. When PHP comes across the <tt>break</tt> keyword,
processing jumps to the next line after the entire <tt>switch</tt> statement. <a class="docLink" href="#learnphpmysql-CHP-4-EX-11">Example 4-11</a> illustrates how processing works with no <tt>break</tt> statements.</p>
<a name="learnphpmysql-CHP-4-EX-11"></a><h5 id="title-IDAVV1EF" class="docExampleTitle">Example 4-11. What happens when there are no break keywords</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
$action="ASSEMBLE ORDER";
switch ($action) {
    case "ASSEMBLE ORDER":
        echo "Perform actions for order assembly.&lt;br&gt;";
    case "PACKAGE":
        echo "Perform actions for packing.&lt;br&gt;";
    case "SHIP":
        echo "Perform actions for shipping.&lt;br&gt;";
}

   echo "Perform actions for shipping.&lt;br&gt;";
}
?&gt;
</pre><br>

</TD></TR></table></P>
<p class="docText">If the value of <tt>$action</tt> is <tt>"ASSEMBLE ORDER"</tt>, the result is:</p>
<pre>
Perform actions for order assembly.
Perform actions for packing.
Perform actions for shipping.
</pre><br>

<p class="docText">However, if a user had already assembled an order, a value of <tt>"PACKAGE"</tt> produces the following:</p>
<pre>
Perform actions for packing.
Perform actions for shipping.
</pre><br>


<a name="learnphpmysql-CHP-4-SECT-3.3.2"></a>
<h5 id="title-IDAPW1EF" class="docSection3Title">4.3.3.2. Defaulting</h5>
<p class="docText">The <tt>case</tt> statement also provides a way to do something if none of the other cases match, which is similar to the <tt>else</tt> statement in an <tt>if, elseif, else</tt> block.</p>
<p class="docText">Use <tt>DEFAULT:</tt> for the switches last <tt>case</tt> statement, as shown in <a class="docLink" href="#learnphpmysql-CHP-4-EX-12">Example 4-12</a>.</p>
<a name="learnphpmysql-CHP-4-EX-12"></a><h5 id="title-IDAKX1EF" class="docExampleTitle">Example 4-12. Using the DEFAULT: statement to generate an error</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
switch ($action) {
    case "ADD":
        echo "Perform actions for adding.";
        break;
    case "MODIFY":
        echo "Perform actions for modifying.";
        break;
    case "DELETE":
        echo "Perform actions for deleting.";
        break;
    default:
        echo "Error: Action must be either ADD, MODIFY, or DELETE.";
}
</pre><br>

</TD></tr></table></p>
<p class="docText">The <tt>switch</tt> statement also supports the alternate syntax in which the <tt>switch</tt> and <tt>endswitch</tt> keywords define the start and end of the switch instead of the curly braces <tt>{}</tt>, as shown in <a class="docLink" href="#learnphpmysql-CHP-4-EX-13">Example 4-13</a>.</P>
<a name="learnphpmysql-CHP-4-EX-13"></a><H5 id="title-IDAGY1EF" class="docExampleTitle">Example 4-13. Using endswitch to end the switch definition</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
switch ($action):
    case "ADD":
       echo "Perform actions for adding.";
       break;
    case "MODIFY":
       echo "Perform actions for modifying.";
       break;
    case "DELETE":
       echo "Perform actions for deleting.";
       break;
    default:
       echo "Error: Action must be either ADD, MODIFY, or DELETE.";
endswitch;
</pre><br>

</TD></tr></table></p>
<p class="docText">You've learned that you can have your programs execute different code based on conditions called expressions. The <tt>switch</tt> statement provides a convenient format for checking the value of an expression against many possible values.</P>



<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
